package com.sgb.kjwl.view.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.swgk.core.view.entity.GoodsPublishEntity;
import com.sgb.kjwl.model.entity.pricecheck.AdmixtureFace;
import com.sgb.kjwl.model.entity.pricecheck.CommonStAttrEntity;
import com.sgb.kjwl.model.entity.pricecheck.Concrete;
import com.sgb.kjwl.model.entity.pricecheck.GoodsAttr;
import com.sgb.kjwl.model.entity.pricecheck.GoodsItem;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.util.MToast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * @author Administrator
 * 2021.03.15 需求变更
 */
public class GoodsInfoView extends LinearLayout implements ItemInputTextView.OnWholeItemClickListener {

    private ItemInputTextView goodsName;
    private ItemInputTextView goodsAttribute;
    private ItemInputTextView goodsBrand;
    private ItemInputTextView goodsUnit;
    private ItemInputTextView goodsAmount;
    private TextView tvDelete;
    private TextView tvEdit;
    private TextView goodsTitle;
    private TextView goodsTitleName;
    private TextView goodsTitleNum;
    private ImageView ivDown;

    private RelativeLayout mRelativeLayout;
    private RelativeLayout rlTitle;
    private LinearLayout llItemContainer;
    private TransferClickEventListener transferClickEventListener;
    private int tag;
    GoodsItem goodsItem;
    Concrete concrete;
    private final Context mContext;

    /**
     * 是否为商砼
     */
    private boolean isSt;

    /**
     * 点击view的回调
     */
    public interface TransferClickEventListener {
        void transfer(int viewId, int tag);
    }

    public void setTransferClickEventListener(TransferClickEventListener listener) {
        this.transferClickEventListener = listener;
    }

    public GoodsInfoView(Context context) {
        super(context);
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.item_add_goods_info, this);
        initView();
        initListener();
    }

    /**
     * 为view绑定model-商品
     * 2021.03.15  18:14
     *
     * @param goodsItem 商品对象
     */
    @Deprecated
    public void setGoodsItem(GoodsItem goodsItem) {
        this.goodsItem = goodsItem;
    }

    /**
     * 为view绑定model-商砼
     * 2021.03.15  18:14
     *
     * @param concrete 商砼对象
     */
    @Deprecated
    public void setConcrete(Concrete concrete) {
        this.concrete = concrete;
    }

    /***
     *为view绑定model
     */
    public void initGoodsModel(Serializable serializable) {
        if (serializable == null) {
            this.goodsItem = new GoodsItem();
            this.concrete = new Concrete();
        } else if (serializable instanceof Concrete) {
            loadData((Concrete) serializable);
            this.goodsItem = new GoodsItem();
        } else if (serializable instanceof GoodsItem) {
            loadData((GoodsItem) serializable);
            this.concrete = new Concrete();
        }
    }

    private void loadData(Concrete concrete) {
        if (concrete == null) {
            this.concrete = new Concrete();
        } else {
            this.concrete = concrete;
            String goodName = concrete.getFcatName()
                    + "-" + concrete.getScatName()
                    + "-" + concrete.getTcatName();
            setGoodsName(goodName);
            setAttribute(getConcreteAttribute(concrete));
            setGoodsAmount(concrete.getNum());
            setBrandVisibility(false);
            setAttributeVisibility(true);
            setConcreteGoodsUnit("立方米");

            setGoodsItemName(goodName, concrete.getNum(), "立方米");
        }
        this.isSt = true;
    }

    /***
     *设置商品属性信息
     * @param content
     */
    private void setAttribute(String content) {
        this.goodsAttribute.setContent(content);
    }

    /***
     * 设置商品的采购量
     * @return
     */
    private void setGoodsAmount(String amount) {
        this.goodsAmount.setContent(amount);
    }

    /****
     * 获取商砼属性值
     * @param concrete
     * @return
     */
    private String getConcreteAttribute(Concrete concrete) {
        StringBuilder stringBuilder = new StringBuilder();
        if (concrete.getStrongLevel() != null) {
            stringBuilder.append(concrete.getStrongLevel()).append(" ");
        }

        if (concrete.getDeliveryMethod() != null) {
            stringBuilder.append("1".equals(concrete.getDeliveryMethod()) ? "一级配" : "二级配").append(" ");
        }
        if (concrete.getConfig() != null) {
            stringBuilder.append("1".equals(concrete.getConfig()) ? "自卸" : "泵送").append(" ");
        }
        List<AdmixtureFace> admixtureFactList = concrete.getAdmixtureFactList();
        if (admixtureFactList != null && admixtureFactList.size() != 0) {
            for (AdmixtureFace admixtureFace : admixtureFactList) {
                if (admixtureFace.getAdmixtureName() != null) {
                    stringBuilder.append(admixtureFace.getAdmixtureName()).append(" ");
                }
            }
        }
        return stringBuilder.toString();
    }

    /***
     * 普通商品
     * @param goodsItem
     */
    private void loadData(GoodsItem goodsItem) {
        if (goodsItem == null) {
            this.goodsItem = new GoodsItem();
        } else {
            this.goodsItem = goodsItem;
            String goodName = goodsItem.getFcatName()
                    + "-" + goodsItem.getScatName()
                    + "-" + goodsItem.getTcatName();
            setGoodsName(goodName);
            String mAttribute = getGoodsItemAttribute(goodsItem);
            if ("".equals(mAttribute)) {
                setAttributeVisibility(false);
            } else {
                setAttribute(mAttribute);
            }
            setGoodsAmount(goodsItem.getNum());
            setBrandName(goodsItem.getBrandName());
            setGoodsUnit(goodsItem.getUnit());
            setGoodsItemName(goodName, goodsItem.getNum(), goodsItem.getUnit());
        }
        this.isSt = false;
    }

    private String getGoodsItemAttribute(GoodsItem goodsItem) {
        List<GoodsAttr> goodsItemAttrList = goodsItem.getAttrList();
        StringBuilder stringBuilder = new StringBuilder();
        if (goodsItemAttrList != null && goodsItemAttrList.size() != 0) {
            for (GoodsAttr goodsAttr : goodsItemAttrList) {
                stringBuilder.append(goodsAttr.getParamValName()).append(" ");
            }
        }
        return stringBuilder.toString();
    }


    private void initListener() {
        tvDelete.setOnClickListener(this::OnWholeItemClick);
        tvEdit.setOnClickListener(this::OnWholeItemClick);
        mRelativeLayout.setOnClickListener(v -> {
            llItemContainer.setVisibility(GONE);
            rlTitle.setVisibility(VISIBLE);
            String goodsName = this.goodsName.getContent();
            String amount = this.goodsAmount.getContent();
            String unit = this.goodsUnit.getContent();
            setGoodsItemName(goodsName, amount, unit);
        });
        ivDown.setOnClickListener(v -> {
            llItemContainer.setVisibility(VISIBLE);
            rlTitle.setVisibility(GONE);
        });
    }

    /****
     *设置单个商品的受说时标题名称
     * @param goodsName 品名
     * @param goodsAmount 采购量
     * @param goodsUnit 单位
     */
    @SuppressLint("SetTextI18n")
    private void setGoodsItemName(String goodsName, String goodsAmount, String goodsUnit) {
        if (goodsName == null || "".equals(goodsName)) {
            goodsTitleName.setText("品名");
            goodsTitleNum.setText("请选择商品(必填)");
            goodsTitleNum.setTextColor(Color.parseColor("#C0C4CC"));
        } else {
            goodsTitleName.setText(goodsName);
            goodsTitleNum.setText(goodsAmount + goodsUnit);
            goodsTitleNum.setTextColor(Color.parseColor("#CC000000"));
        }
    }

    private void initView() {
        goodsName = findViewById(R.id.goodsName);
        goodsAttribute = findViewById(R.id.goodsAttribute);
        goodsBrand = findViewById(R.id.goodsBrand);
        goodsUnit = findViewById(R.id.goodsUnit);
        goodsAmount = findViewById(R.id.goodsAmount);
        goodsTitle = findViewById(R.id.goodsTitle);
        goodsAmount.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

        tvDelete = findViewById(R.id.tv_delete);
        tvDelete.setVisibility(GONE);

        tvEdit = findViewById(R.id.tv_edit);

        goodsTitleName = findViewById(R.id.tv_title_name);
        goodsTitleNum = findViewById(R.id.tv_title_num);

        goodsTitleName.setText("品名");
        goodsTitleNum.setText("请选择商品(必填)");
        goodsTitleNum.setTextColor(Color.parseColor("#C0C4CC"));

        ivDown = findViewById(R.id.tv_title_down);
        rlTitle = findViewById(R.id.rl_item_title);
        rlTitle.setVisibility(VISIBLE);
        mRelativeLayout = findViewById(R.id.rl_up);
        llItemContainer = findViewById(R.id.ll_item_container);
        llItemContainer.setVisibility(GONE);
    }

    /**
     * 重新选取商品后重置各个属性
     */
    @Deprecated
    public void resetGoodsAttribute() {
        goodsBrand.setContent("");
        goodsAttribute.setContent("");
        goodsUnit.setContent("");
        goodsAmount.setContent("");
//        goodsBrand.setContent(""); //2021 03.13这可能是个问题
    }

    /**
     * 设置当前view的位置
     *
     * @param tag
     */
    public void setCurrentViewTag(int tag) {
        this.tag = tag;
    }

    /**
     * 获取当前view的位置
     */
    public int getCurrentViewTag() {
        return this.tag;
    }

    @Override
    public void OnWholeItemClick(View view) {
        transferClickEventListener.transfer(view.getId(), getCurrentViewTag());
    }

    /**
     * 商品个数的标题
     *
     * @param title 标题
     */
    public void setTitle(String title) {
        this.goodsTitle.setText(title);
    }

    /**
     * 设置商品的名字
     *
     * @param content 选框选取后商品的名字
     */
    public void setGoodsName(String content) {
        this.goodsName.setContent(content);
    }

    /**
     * 选择的商品名字
     *
     * @return 商品名字
     */
    public String getGoodsName() {
        return this.goodsName.getContent();
    }

    /**
     * 设置UI商品的属性内容
     *
     * @param content 商品的属性
     */
    public void setGoodsAttribute(String content) {
        this.goodsAttribute.setContent(content);
    }

    /**
     * 设置商品的单位
     *
     * @param content 选框选取的品牌
     */
    public void setGoodsUnit(String content) {
        this.goodsUnit.setContent(content);
        goodsItem.setUnit(content);
    }

    /**
     * 设置商砼的单位
     *
     * @param content “立方米”
     */
    public void setConcreteGoodsUnit(String content) {
        this.goodsUnit.setContent(content);
        concrete.setUnit(content);
    }

    /**
     * 设置商品品牌
     *
     * @param content 选框选取的品牌
     */
    public void setBrandName(String content) {
        if (!TextUtils.isEmpty(content)) {
            this.goodsBrand.setContent(content);
            setBrandVisibility(true);
        } else {
            setBrandVisibility(false);
        }

        goodsItem.setBrandName(content);
    }

    /**
     * 获取采购量
     *
     * @return 采购量
     */
    public String getGoodsAmount() {
        return this.goodsAmount.getContent();
    }

    /**
     * 获取商品实体类对象
     *
     * @return 商品实体类对象
     */
    public GoodsItem getGoodsItem() {
        return goodsItem;
    }

    /**
     * 获取商砼实体类对象
     *
     * @return 商砼实体类对象
     */
    public Concrete getConcrete() {
        return concrete;
    }

    public void setDeleteVisibility(boolean isShow) {
        this.tvDelete.setVisibility(isShow ? VISIBLE : GONE);
    }

    /**
     * 设置属性集合
     *
     * @param attrList
     */
    public void setAttrList(List<GoodsAttr> attrList) {
        goodsItem.setAttrList(attrList);
    }

    /**
     * 构建商品基本属性
     *
     * @param goodsPublishEntity 选择具体商品后的商品对象
     */
    @Deprecated
    public void buildGoodsItem(GoodsPublishEntity goodsPublishEntity) {
        goodsItem.setBrandName(goodsPublishEntity.getBrandName());
        goodsItem.setFcatName(goodsPublishEntity.getCid1Name());
        goodsItem.setFcategory(goodsPublishEntity.getCid1());
        goodsItem.setScatName(goodsPublishEntity.getCid2Name());
        goodsItem.setScategory(goodsPublishEntity.getCid2());
        goodsItem.setTcatName(goodsPublishEntity.getCid3Name());
        goodsItem.setTcategory(goodsPublishEntity.getCid3());
    }

    /**
     * 构建商砼基本属性
     *
     * @param goodsPublishEntity 选择商砼后的商品对象
     */
    @Deprecated
    public void buildConcrete(GoodsPublishEntity goodsPublishEntity) {
        concrete.setFcatName(goodsPublishEntity.getCid1Name());
        concrete.setFcategory(goodsPublishEntity.getCid1());
        concrete.setScatName(goodsPublishEntity.getCid2Name());
        concrete.setScategory(goodsPublishEntity.getCid2());
        concrete.setTcatName(goodsPublishEntity.getCid3Name());
        concrete.setTcategory(goodsPublishEntity.getCid3());
    }

    /**
     * 当前选择是否为商砼
     *
     * @param isSt 是否为商砼
     */
    @Deprecated
    public void setIsSt(boolean isSt) {
        this.isSt = isSt;
    }

    public boolean isSt() {
        return this.isSt;
    }

    /**
     * 构建商砼的属性信息
     *
     * @param key           属性标题
     * @param admixtureList 加强剂属性
     * @param otherAttr     其他属性
     * @param stringBuilder 拼接显示属性的builder
     */
    @Deprecated
    public void buildConcreteAttribute(String key, List<CommonStAttrEntity.ChildListBean> admixtureList, CommonStAttrEntity.ChildListBean otherAttr, StringBuilder stringBuilder) {
        if ("strength".equals(key)) {
            stringBuilder.append(otherAttr.getName()).append(" ");
            concrete.setStrongLevel(otherAttr.getName());
        }
        if ("config".equals(key)) {
            stringBuilder.append(otherAttr.getName()).append(" ");
            concrete.setConfig(TextUtils.equals(otherAttr.getName(), "一级配") ? "1" : "2");
        }
        if ("deliveryMethod".equals(key)) {
            stringBuilder.append(otherAttr.getName()).append(" ");
            concrete.setDeliveryMethod(TextUtils.equals(otherAttr.getName(), "自卸") ? "1" : "2");
        }
        if ("admixture".equals(key)) {
            List<AdmixtureFace> list = new ArrayList<>();
            for (CommonStAttrEntity.ChildListBean admixtureAttr : admixtureList) {
                stringBuilder.append(admixtureAttr.getName()).append(" ");
                AdmixtureFace admixtureFace = new AdmixtureFace();
                admixtureFace.setAdmixtureName(admixtureAttr.getName());
                admixtureFace.setSpec(String.valueOf(admixtureAttr.getSpec()));
                list.add(admixtureFace);
            }
            concrete.setAdmixtureFactList(list);
        }
        setGoodsAttribute(stringBuilder.toString());
    }

    /**
     * 设置品牌条目的可见性（商砼 无品牌属性）
     *
     * @param visibility 可见性
     */
    public void setBrandVisibility(boolean visibility) {
        goodsBrand.setVisibility(visibility ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置商品属性条目的可见性
     *
     * @param visibility 可见性
     */
    public void setAttributeVisibility(boolean visibility) {
        goodsAttribute.setVisibility(visibility ? View.VISIBLE : View.GONE);
        if (!visibility) {
            setAttrList(new ArrayList<>());
        }
    }

    /**
     * 校验页面输入是否合规
     *
     * @return 是否合规
     */
    public boolean isInputValid() {
        if (TextUtils.isEmpty(goodsName.getContent())) {
            MToast.showToast(mContext, "请编辑商品");
            return false;
        }
        if (goodsAttribute.getVisibility() != View.GONE) {
            if (TextUtils.isEmpty(goodsAttribute.getContent())) {
                MToast.showToast(mContext, "请编辑商品属性");
                return false;
            }
        }
//        if (goodsBrand.getVisibility() != View.GONE) {
//            if (TextUtils.isEmpty(goodsBrand.getContent())) {
//                MToast.showToast(mContext, "请编辑品牌");
//                return false;
//            }
//        }
        if (TextUtils.isEmpty(goodsAmount.getContent())) {
            MToast.showToast(mContext, "请编辑商品数量");
            return false;
        }
        if (TextUtils.isEmpty(goodsUnit.getContent())) {
            MToast.showToast(mContext, "请编辑单位");
            return false;
        }
        return true;
    }
}
